/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import 'package:flutter/material.dart';

class CardItem extends StatefulWidget {
  final double currentWidth;
  final String name;
  final String message;
  final String ava;
  final String? messageNum;
  final String activeName;
  final Function activeCallBack;

  const CardItem(
      {Key? key,
      required this.currentWidth,
      required this.name,
      required this.message,
      required this.ava,
      this.messageNum,
      required this.activeCallBack,
      required this.activeName})
      : super(key: key);

  @override
  State<CardItem> createState() => _CardItemState();
}

class _CardItemState extends State<CardItem> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
        onTap: () => widget.activeCallBack(widget.name),
        child: Container(
          width: widget.currentWidth,
          margin: const EdgeInsets.all(10),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(15),
              color:
                  widget.name == widget.activeName ? const Color(0xFFE6EEFE) : null),
          child: Row(
            children: [
              SizedBox(
                width: 55,
                child: Stack(
                  children: [
                    Container(
                      margin: const EdgeInsets.only(left: 10),
                      child: Image.asset(widget.ava),
                    ),
                    if (widget.messageNum != null)
                      Positioned(
                          right: 0,
                          top: 0,
                          child: Row(
                              mainAxisSize: MainAxisSize.min, // 关键：宽度由子控件决定
                              children: [
                                Container(
                                    height: 16,
                                    decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(8),
                                        border: Border.all(
                                            color: Colors.white, width: 2),
                                        color: const Color(0xFFE84826)),
                                    child: Column(
                                      children: [
                                        Container(
                                          padding: const EdgeInsets.only(
                                              left: 5, right: 5),
                                          child: const Text(
                                            '99+',
                                            style: TextStyle(
                                              fontSize: 9,
                                              color: Colors.white,
                                            ),
                                            textAlign: TextAlign.center,
                                          ),
                                        )
                                      ],
                                    ))
                              ]))
                  ],
                ),
              ),
              Expanded(
                child: Container(
                  padding:
                      const EdgeInsets.only(left: 0, right: 14, top: 14, bottom: 14),
                  decoration: const BoxDecoration(
                      border: Border(
                    bottom: BorderSide(width: 1.0, color: Color(0xFFF2F2F2)),
                  )),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: const EdgeInsets.only(left: 10),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              widget.name,
                              style: const TextStyle(
                                  fontSize: 16, fontWeight: FontWeight.w500),
                            ),
                            Text(
                              widget.message,
                              style: const TextStyle(
                                  fontSize: 14, color: Color(0xFF666666)),
                            )
                          ],
                        ),
                      ),
                      const Text(
                        '13:35',
                        style:
                            TextStyle(color: Color(0xFF182431), fontSize: 12),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ));
  }
}
